package com.ajview.common.core.domain.bo;

import com.ajview.common.core.domain.eneity.SysUser;
import com.ajview.common.core.enums.UserStatus;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;

/**
 * @author ZhangJunJie
 * @Date 2022-04-27
 * <p>
 * 概要：业务对象、认证业务对象模型
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginUser implements UserDetails, Serializable {

    private static final long serialVersionUID = 787798240778809843L;
    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 部门ID
     */
    private Long deptId;

    /**
     * 用户唯一标识
     */
    private String tokenUUID;

    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 登录地点
     */
    private String loginLocation;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 用户信息
     */
    private SysUser sysUser;

    /**
     * 权限信息
     */
    private Set<String> permissions;

    public LoginUser(Long userId, Long deptId, SysUser sysUser, Set<String> permissions) {
        this.userId = userId;
        this.deptId = deptId;
        this.sysUser = sysUser;
        this.permissions = permissions;
    }

    @Override
    /**
     * 忽略序列化和反序列化
     */
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }

    @Override
    public String getUsername() {
        return sysUser.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return UserStatus.OK.getCode().equals(sysUser.getStatus());
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return UserStatus.OK.getCode().equals(sysUser.getStatus());
    }
}
